/*
 * ServerWindow.java
 *
 * Created on 10. říjen 2008, 14:54
 */

package gui;

import interfaces.Core_Events;
/**
 *
 * @author  Groš
 */
public class ServerWindow extends javax.swing.JFrame {

	public ServerWindow() {
            setSystemLook();
            initComponents();
	}

	
    /** Creates new form ServerWindow */
    public ServerWindow(Core_Events event) {
        setSystemLook();
        initComponents();
        this.event = event;
        tDownloads.setText(core.Core.DOWNLOADS_DIRECTORY);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tPort = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tIP = new javax.swing.JTextField();
        bStart = new javax.swing.JButton();
        bStop = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tText = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        tDownloads = new javax.swing.JTextField();
        tReceived = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Server");

        jLabel1.setText("Listen on port:");

        tPort.setText("5005");

        jLabel2.setText("Listen on IP:");

        tIP.setText("127.0.0.1");

        bStart.setText("Start");
        bStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bStartActionPerformed(evt);
            }
        });

        bStop.setText("Stop");
        bStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bStopActionPerformed(evt);
            }
        });

        tText.setColumns(20);
        tText.setEditable(false);
        tText.setFont(new java.awt.Font("Arial", 1, 10));
        tText.setRows(5);
        jScrollPane1.setViewportView(tText);

        jLabel3.setText("Download directory:");

        tDownloads.setEditable(false);

        tReceived.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tReceived.setText("Received: 0 b");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tIP, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                            .addComponent(tPort, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                            .addComponent(tDownloads, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(bStart, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bStop, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(tReceived, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tDownloads, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bStop)
                    .addComponent(bStart))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tReceived))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void bStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bStopActionPerformed
    event.core_message("SW:Stop");
}//GEN-LAST:event_bStopActionPerformed

private void bStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bStartActionPerformed
    event.core_message("SW:Start");
}//GEN-LAST:event_bStartActionPerformed

private void setSystemLook() {
    try {
      javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
    } 
    catch(Exception e) {
      System.out.println("Error setting native look: " + e);
    }
    this.setVisible(true);
}

public String getIP(){
    return tIP.getText();
}

public String getPort(){
    return tPort.getText();
}

public void writeText(String text) {
    tText.append(text + "\n");
}

public void clearText() {
    tText.setText("");
}

/** expect only number of bytes */
public void changeStateLine(final String text) {
    //final String ftext = text;
    //java.awt.EventQueue.invokeLater(new Runnable() {
      //      public void run() {
                tReceived.setText("Received: " + text + " b");
        //    }
        //});
}

public void show_StartButton(boolean show) {
    bStart.setEnabled(show);
}

public boolean isStartButtonEnabled() {
    return bStart.isEnabled();
}

public boolean isStopButtonEnabled() {
    return bStop.isEnabled();
}


public void show_StopButton(boolean show) {
    bStop.setEnabled(show);
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bStart;
    private javax.swing.JButton bStop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tDownloads;
    private javax.swing.JTextField tIP;
    private javax.swing.JTextField tPort;
    private javax.swing.JLabel tReceived;
    private javax.swing.JTextArea tText;
    // End of variables declaration//GEN-END:variables
    private Core_Events event;


	public String getText(){
		return tText.getText();
	}
}
